package com.woniu.log;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class LogAdvice {
    @Around("@annotation(com.woniu.log.WoniuLog)")
    public Object logInfo(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature)pjp.getSignature();
        WoniuLog methodName = signature.getMethod().getDeclaredAnnotation(WoniuLog.class);
        log.info("========开始执行{}=======", methodName.value());
        log.info("请求参数：{}", pjp.getArgs());
        Object result = pjp.proceed();
        log.info("请求结果：{}", JSONUtil.parse(result).toString());
        log.info("========{}执行结束，耗时{}ms=======", methodName.value(), System.currentTimeMillis() - start);
        return result;
    }
}
